// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db=cloud.database()
const _=db.command;
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  let openid = wxContext.OPENID;
  let wdpm='未上榜';
  let myData;
  const{t1,t2}=event;

  if(t1=='run'){
    let pro;
    switch(t2){
      case 'day':
        pro = await db.collection('stepPhb').orderBy('dayData','desc').field({
          weekData:false,
          monthData:false,
          wup:false,
          mup:false
        }).get();
      break;
      case 'week':
        pro = await db.collection('stepPhb').orderBy('weekData.ws', 'desc').field({
          dayData:false,
          monthData:false,
          dup:false,
          mup:false
        }).get();
      break;
      case 'month':
        pro = await db.collection('stepPhb').orderBy('monthData.ms', 'desc').field({
          dayData:false,
          weekData:false,
          dup:false,
          wup:false
        }).get();
      break;
    }

    let data=pro.data;

    //获得点赞数和当前用户点赞状态
    data = data.map((v, k) => {
      let up = v.dup ? v.dup : (v.wup ? v.wup : v.mup);
      v.upNum = up.length;
      v.curUp = false;
      if (up.find(item => { return item == openid })) {
        v.curUp = true;
      }
      return v;
    })

    //检索列表的好友
    let prof=await db.collection('friend').where({
      openid
    }).field({friends:true}).get();
    let friends=prof.data[0].friends;
    data = data.map((v, k) => {
      v.isFriend=false;
      if(friends.find(item=>{return item==v.openid})){
        v.isFriend=true
      }
      return v;
    })


    //获取当前用户排名和总步数
    for(let i=0;i<data.length;i++){
      if(data[i].openid==openid){
        wdpm=i+1;
        myData=data[i];
        break;
      }
    }

    return{
      wdpm,
      myData,
      data
    }
  }

  else{
    let pro;
    switch (t2) {
      case 'day':
        pro = await db.collection('giftPhb').orderBy('dayRec', 'desc').field({
          weekRec: false,
          monthRec: false,
          wup: false,
          mup: false
        }).get();
        break;
      case 'week':
        pro = await db.collection('giftPhb').orderBy('weekRec', 'desc').field({
          dayRec: false,
          monthRec: false,
          dup: false,
          mup: false
        }).get();
        break;
      case 'month':
        pro = await db.collection('giftPhb').orderBy('monthRec', 'desc').field({
          dayRec: false,
          weekRec: false,
          dup: false,
          wup: false
        }).get();
        break;
    }

    let data = pro.data;

    //获得点赞数和当前用户点赞状态
    data = data.map((v, k) => {
      let up = v.dup ? v.dup : (v.wup ? v.wup : v.mup);
      v.upNum = up.length;
      v.curUp = false;
      if (up.find(item => { return item == openid })) {
        v.curUp = true;
      }
      return v;
    })

    //检索列表的好友
    let prof = await db.collection('friend').where({
      openid
    }).field({ friends: true }).get();
    let friends = prof.data[0].friends;
    data = data.map((v, k) => {
      v.isFriend = false;
      if (friends.find(item => { return item == v.openid })) {
        v.isFriend = true
      }
      return v;
    })


    //获取当前用户排名和总步数
    for (let i = 0; i < data.length; i++) {
      if (data[i].openid == openid) {
        wdpm = i + 1;
        myData = data[i];
        break;
      }
    }

    return {
      wdpm,
      myData,
      data
    }
  }
}